package com.egreen.user.service.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.egreen.common.pojo.DTO.user.userServiceDTO.SendVerificationCodeDTO;
import com.egreen.common.pojo.VO.user.UserVO;
import com.egreen.common.pojo.entity.user.LocationDetail;
import com.egreen.common.pojo.entity.user.User;
import com.egreen.common.pojo.entity.user.UserLocation;
import com.egreen.common.pojo.entity.user.UserLoginAccount;
import com.egreen.user.exceptions.UserException;
import com.egreen.user.dao.LocationDetailDao;
import com.egreen.user.dao.UserDao;
import com.egreen.user.dao.UserLocationDao;
import com.egreen.user.dao.UserLoginAccountDao;
import com.egreen.user.service.IUserService;
import com.egreen.common.pojo.DTO.user.ChangeStateDTO;
import com.egreen.common.serviceapi.user.IUserServiceApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLClientInfoException;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Component
public class UserServiceApi implements IUserServiceApi {
    @Autowired
    IUserService userService;

    @Autowired
    UserLoginAccountDao userLoginAccountDao;
    /**
     * 用户模块中需要提供给订单模块的方法  yyf
     */
    @Autowired
    private UserLocationDao userLocationDao;
    @Autowired
    private LocationDetailDao locationDetailDao;
    @Autowired
    private UserDao userDao;

    /**
     * 查看用户列表
     *
     * @return
     */
    @Override
    public List<UserVO> selectUserList(int states) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "user_name", "icon_url", "states", "point_account_id", "balance_account_id", "location_id", "invited_number");
        queryWrapper.eq("states", states);
        //将查询到的数据封装到集合当中
        List<User> list = userDao.selectList(queryWrapper);
        //因为查出来的集合为User的集合，故需要放在新集合UserVO里
        List<UserVO> returnList = new ArrayList<>();
        return returnList;
    }

    @Override
    @Transactional(rollbackFor = {SQLClientInfoException.class, UserException.class})
    public void changeState(ChangeStateDTO changeStateDTO) {
        User user = new User();
        user.setId(changeStateDTO.getUserID());
        switch (changeStateDTO.getState()) {
            case "普通用户":
                user.setStates(0);
                break;
            case "社区代理":
                user.setStates(1);
                break;
            case "区域代理":
                user.setStates(2);
                break;
            case "城市代理":
                user.setStates(3);
                break;
            case "社区代理(待审核)":
                user.setStates(4);
                break;
            case "区域代理(待审核)":
                user.setStates(5);
                break;
            case "城市代理(待审核)":
                user.setStates(6);
                break;
        }
        User user1 = new User();
        BeanUtils.copyProperties(user, user1);
        userService.updateById(user1);
    }

    @Override
    public String sendValidCodeSms(String sendTo) {
        userService.sendVerificationCode(new SendVerificationCodeDTO(sendTo));
        return null;
    }


    /**
     * 用户模块提供给订单模块的接口实现 yyf
     *
     * @param locationId
     * @return
     */
    @Override
    public UserLocation selectLocationById(Long locationId) {
        UserLocation userLocation = userLocationDao.selectById(locationId);
        UserLocation userLocation1 = new UserLocation();
        BeanUtils.copyProperties(userLocation, userLocation1);
        return userLocation1;
    }

    @Override
    public LocationDetail selectLocationDetailById(Long detailId) {
        LocationDetail locationDetail = locationDetailDao.selectById(detailId);
        LocationDetail locationDetail1 = new LocationDetail();
        BeanUtils.copyProperties(locationDetail, locationDetail1);
        return locationDetail1;
    }

    @Override
    public List<User> getUsersFromCity(Integer provinceId, String city) {
        QueryWrapper<UserLocation> wrapper = new QueryWrapper();
        wrapper.eq("city_name", city);
        wrapper.eq("province_id", provinceId);
        List<UserLocation> list = userLocationDao.selectList(wrapper);
        List<User> users = new ArrayList<>();

        //获取当前地区下的所有用户
        for (UserLocation location : list) {
            QueryWrapper<User> userWrapper = new QueryWrapper<>();
            userWrapper.eq("location_id", location.getId());
            List<User> users1 = userDao.selectList(userWrapper);
            for (User user : users1) {
                users.add(user);
            }
        }
        return users;
    }

    @Override
    public List<User> getUsersFromDetail(Integer provinceId, String city, String district, String community) {
        List<User> usersFromCity = this.getUsersFromCity(provinceId, city);
        List<User> users = new ArrayList<>();

        //二次过滤
        for (User u : usersFromCity) {
            //获取位置细节信息
            UserLocation userLocation =
                    userLocationDao.selectById(u.getLocationId());
            LocationDetail locationDetail =
                    locationDetailDao.selectById(userLocation.getDetailId());
            if (locationDetail.getDistrict().equals(district)) {
                if (community.isEmpty()) {
                    users.add(u);
                } else {
                    if (locationDetail.getCommunity().equals(community)) {
                        users.add(u);
                    }
                }
            }
        }

        return users;
    }

    @Override
    public User selectUserById(Long id) {
        User user = userDao.selectById(id);
        return user;
    }

    /**
     * 改变用户状态
     *
     * @param id
     */
    @Override
    public void changeUserStatus(int id) {
        User user = userDao.selectById(id);
        user.setStates(0);

        userDao.updateById(user);
    }

    @Override
    public UserLoginAccount selectLoginById(Long id) {
        UserLoginAccount userLoginAccount = userLoginAccountDao.selectById(id);
        return userLoginAccount;
    }


}
